package com.graduation.table.administrators.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.graduation.base.Result;
import com.graduation.table.administrators.entity.Administrators;
import com.graduation.table.administrators.service.IAdministratorsService;
import com.graduation.table.administrators.vo.AdministratorsVo;
import com.graduation.table.administratorsrole.relation.entity.AdministratorsRoleRelation;
import com.graduation.table.administratorsrole.relation.service.IAdministratorsRoleRelationService;
import com.graduation.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 管理员
 * @Author: jeecg-boot
 * @Date:   2022-04-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags="管理员")
@RestController
@RequestMapping("/administrators")
public class AdministratorsController{
	@Resource
	private IAdministratorsService administratorsService;
	@Resource
	private IAdministratorsRoleRelationService administratorsRoleRelationService;

	 /**
	  * 管理员登录接口
	  *
	  */
	 @ApiOperation(value="管理员-管理员登录接口", notes="管理员-管理员登录接口")
	 @PostMapping(value = "/login")
	 public Result<?> queryPageList(@RequestBody AdministratorsVo administratorsVo) {
	 	LambdaQueryWrapper<Administrators> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	 	lambdaQueryWrapper.eq(Administrators::getUsername,administratorsVo.getUsername());
	 	Administrators trueAdministrators = administratorsService.getOne(lambdaQueryWrapper);
	 	if (Objects.isNull(trueAdministrators)){
	 		return Result.error("不存在该用户");
		}
	 	if (trueAdministrators.getPassword().equals(administratorsVo.getPassword())){
	 		return Result.OK(trueAdministrators.getId());
		}else {
	 		return Result.error("密码错误");
		}

	 }

	 /**
	  * 管理员注册接口
	  *
	  */
	 @ApiOperation(value="管理员-管理员注册接口", notes="管理员-管理员注册接口")
	 @PostMapping(value = "/register")
	 @Transactional
	 public Result<?> register(@RequestBody AdministratorsVo administratorsVo) {
		 LambdaQueryWrapper<Administrators> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		 lambdaQueryWrapper.eq(Administrators::getUsername,administratorsVo.getUsername());
		 Administrators trueAdministrators = administratorsService.getOne(lambdaQueryWrapper);
		 if (Objects.nonNull(trueAdministrators)){
			 return Result.error("该管理员账号已经存在");
		 }
		 if (StringUtils.isEmpty(administratorsVo.getPassword()) ||
				 StringUtils.isEmpty(administratorsVo.getUsername()) ||
				 StringUtils.isEmpty(administratorsVo.getPhone())){
		 	return Result.error("用户名，密码，手机号不能为空");
		 }
		 Administrators administrators = new Administrators();
		 administrators.setUsername(administratorsVo.getUsername());
		 administrators.setPassword(administratorsVo.getPassword());
		 administrators.setPhone(administratorsVo.getPhone());
		 administratorsService.save(administrators);


		 AdministratorsRoleRelation administratorsRoleRelation = new AdministratorsRoleRelation();
		 administratorsRoleRelation.setAdministratorsId(administrators.getId());
		 administratorsRoleRelation.setRoleId("2");

		 administratorsRoleRelationService.save(administratorsRoleRelation);

		 return Result.OK("注册成功");

	 }
	
	/**
	 * 分页列表查询
	 *
	 */
	@ApiOperation(value="管理员-分页列表查询", notes="管理员-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(String search,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<Administrators> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
				.like(!StringUtils.isEmpty(search), Administrators::getUsername,search)
				.or()
				.like(!StringUtils.isEmpty(search),Administrators::getPhone,search)
		;
		List<Administrators> list = administratorsService.list(queryWrapper);
		PageUtil pageUtil = PageUtil.doPage(pageNo,pageSize,list);
		return Result.OK(pageUtil);
	}
	
	/**
	 * 添加
	 *
	 */
	@ApiOperation(value="管理员-添加", notes="管理员-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Administrators administrators) {
		administratorsService.save(administrators);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 */
	@ApiOperation(value="管理员-编辑", notes="管理员-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Administrators administrators) {
		administratorsService.updateById(administrators);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 */
	@ApiOperation(value="管理员-通过id删除", notes="管理员-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id") String id) {
		administratorsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 */
	@ApiOperation(value="管理员-批量删除", notes="管理员-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids") String ids) {
		this.administratorsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 */
	@ApiOperation(value="管理员-通过id查询", notes="管理员-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id") String id) {
		Administrators administrators = administratorsService.getById(id);
		return Result.OK(administrators);
	}

}
